﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections.ObjectModel;


namespace MyChars
{
    class Utils
    {
#if false
        //http://www.codeproject.com/Articles/2118/Bypass-Graphics-MeasureString-limitations
        public int MeasureDisplayStringWidth(Graphics graphics, string text, Font font)
        {
            const int width = 32;

            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, 1, graphics);
            System.Drawing.SizeF size = graphics.MeasureString(text, font);
            System.Drawing.Graphics anagra = System.Drawing.Graphics.FromImage(bitmap);

            int measured_width = (int)size.Width;

            if (anagra != null)
            {
                anagra.Clear(Color.White);
                anagra.DrawString(text + "|", font, Brushes.Black, width - measured_width, -font.Height / 2);

                for (int i = width - 1; i >= 0; i--)
                {
                    measured_width--;
                    if (bitmap.GetPixel(i, 0).R != 255)    // found a non-white pixel ?
                        break;
                }
            }

            return measured_width;
        }

        //http://stackoverflow.com/questions/4820212/automatically-trim-a-bitmap-to-minimum-size
        //http://stackoverflow.com/questions/4820212/automatically-trim-a-bitmap-to-minimum-size

        //http://stackoverflow.com/questions/4820212/automatically-trim-a-bitmap-to-minimum-size
        public int MeasureDisplayStringHeight(Graphics graphics, string text, Font font)
        {
            text = "A";
            SizeF size = graphics.MeasureString(text, font);
            int fontHeight = (int)Math.Ceiling(size.Height) + 1;
            int fontWidth = (int)Math.Ceiling(size.Width) + 1;

            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fontWidth, fontHeight, graphics);
            System.Drawing.Graphics anagra = System.Drawing.Graphics.FromImage(bitmap);

            int empty_lines_top = 0;
            int empty_lines_bottom = 0;
            if (anagra != null)
            {
                anagra.Clear(Color.Black);
                anagra.DrawString(text, font, Brushes.Red, 0, 0);
                string s = "";
                for (int y = 0; y < fontHeight; y++)
                {
                    for (int x = 0; x < fontWidth; x++)
                    {
                        s += (bitmap.GetPixel(x, y).R != 0) ? "X" : "_";
                    }
                    s += "\r\n";
                }
                MessageBox.Show(s);
                return 0;

                for (int i = 0; i < fontHeight; i++)
                {
                    for (int k = 1; k < fontWidth - 1; k++)
                    {
                        if (bitmap.GetPixel(k, i).R != 0)    // found a non-white pixel ?
                            break;
                    }
                    empty_lines_top++;
                }

                for (int i = fontHeight - 1; i >= 0; i--)
                {
                    for (int k = 1; k < fontWidth - 1; k++)
                    {
                        if (bitmap.GetPixel(k, i).R != 0)    // found a non-white pixel ?
                            break;
                    }
                    empty_lines_bottom++;
                }
            }

            return fontHeight - (empty_lines_top + empty_lines_bottom);
        }


        static Bitmap TrimBitmap(Bitmap source)
        {
            Rectangle srcRect = default(Rectangle);
            BitmapData data = null;
            try
            {
                data = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                byte[] buffer = new byte[data.Height * data.Stride];
                Marshal.Copy(data.Scan0, buffer, 0, buffer.Length);

                int xMin = int.MaxValue,
                    xMax = int.MinValue,
                    yMin = int.MaxValue,
                    yMax = int.MinValue;

                bool foundPixel = false;

                // Find xMin 
                for (int x = 0; x < data.Width; x++)
                {
                    bool stop = false;
                    for (int y = 0; y < data.Height; y++)
                    {
                        byte alpha = buffer[y * data.Stride + 4 * x + 3];
                        if (alpha != 0)
                        {
                            xMin = x;
                            stop = true;
                            foundPixel = true;
                            break;
                        }
                    }
                    if (stop)
                        break;
                }

                // Image is empty... 
                if (!foundPixel)
                    return null;

                // Find yMin 
                for (int y = 0; y < data.Height; y++)
                {
                    bool stop = false;
                    for (int x = xMin; x < data.Width; x++)
                    {
                        byte alpha = buffer[y * data.Stride + 4 * x + 3];
                        if (alpha != 0)
                        {
                            yMin = y;
                            stop = true;
                            break;
                        }
                    }
                    if (stop)
                        break;
                }

                // Find xMax 
                for (int x = data.Width - 1; x >= xMin; x--)
                {
                    bool stop = false;
                    for (int y = yMin; y < data.Height; y++)
                    {
                        byte alpha = buffer[y * data.Stride + 4 * x + 3];
                        if (alpha != 0)
                        {
                            xMax = x;
                            stop = true;
                            break;
                        }
                    }
                    if (stop)
                        break;
                }

                // Find yMax 
                for (int y = data.Height - 1; y >= yMin; y--)
                {
                    bool stop = false;
                    for (int x = xMin; x <= xMax; x++)
                    {
                        byte alpha = buffer[y * data.Stride + 4 * x + 3];
                        if (alpha != 0)
                        {
                            yMax = y;
                            stop = true;
                            break;
                        }
                    }
                    if (stop)
                        break;
                }

                srcRect = Rectangle.FromLTRB(xMin, yMin, xMax, yMax);
            }
            finally
            {
                if (data != null)
                    source.UnlockBits(data);
            }

            Bitmap dest = new Bitmap(srcRect.Width, srcRect.Height);
            Rectangle destRect = new Rectangle(0, 0, srcRect.Width, srcRect.Height);
            using (Graphics graphics = Graphics.FromImage(dest))
            {
                graphics.DrawImage(source, destRect, srcRect, GraphicsUnit.Pixel);
            }
            return dest;
        } 


#endif
    }//class
}//namespace
